package com.heima.behavior.service.impl;
import java.util.Date;

import com.heima.behavior.dto.BehaviorDto;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.entity.ApFollowBehavior;
import com.heima.behavior.mapper.ApFollowBehaviorMapper;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.heima.behavior.service.IApFollowBehaviorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP关注行为表 服务实现类
 * </p>
 *
 * @author lj
 * @since 2022-07-05
 */
@Slf4j
@Service
public class ApFollowBehaviorServiceImpl extends ServiceImpl<ApFollowBehaviorMapper, ApFollowBehavior> implements IApFollowBehaviorService {

    @Autowired
    private IApBehaviorEntryService apBehaviorEntryService;

    /**
     * 保存关注行为
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveFollow(BehaviorDto dto) {
        log.info("[用户行为微服务======>保存关注行为实现类，接收参数：{}]",dto.toString());
        // 参数判断
        if (dto==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 获取用户行为实体
        ApBehaviorEntry entry = apBehaviorEntryService.getEntryByUserIdOrEquipmentId(dto.getUserId(),dto.getEquipmentId());
        // 构建关注行为对象
        ApFollowBehavior apFollowBehavior = new ApFollowBehavior();
        // apFollowBehavior.setId(0L);
        apFollowBehavior.setEntryId(entry.getId());
        apFollowBehavior.setFollowId(dto.getAuthorId());
        apFollowBehavior.setOperation(dto.getOperation());
        apFollowBehavior.setCreatedTime(new Date());
        // 保存
        this.save(apFollowBehavior);
        // 返回结果
        return ResponseResult.okResult();
    }
}
